package com.hdu.controller;


import com.hdu.req.TaskTriggerReq;
import com.hdu.result.R;
import com.hdu.manager.DelayTaskManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.hdu.util.ParamChecker.unValidParam;

@RequestMapping("task")
public class TaskTriggerController {


    private final DelayTaskManager delayTaskManager;

    public TaskTriggerController(DelayTaskManager delayTaskManager) {
        this.delayTaskManager = delayTaskManager;
    }

    @PostMapping("trigger")
    @ResponseBody
    public R trigger(@RequestBody TaskTriggerReq req) {
        unValidParam(
                StringUtils.isEmpty(req.getTaskName()) || StringUtils.isEmpty(req.getTaskParam()),
                "taskName or taskParam is empty"
        );

        return delayTaskManager.trigger(req.getTaskName(), req.getTaskParam());
    }
}
